/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package apresentacao.obra;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import modelo_relacional.Carro;
import modelo_relacional.Obra;
import modelo_relacional.Obra_Item;
import service.CarroService;
import service.ObraService;

/**
 *
 * @author Rui Pereira
 */
public class ObraFrm extends javax.swing.JFrame {
    private CarroService carroSevice;
    private ObraService obraService;
    private Obra obra;
    private Obra_Item obraItem;
    private Carro carro;
    List<Carro> carros;
    /**
     * Creates new form ObraFrm
     */
    public ObraFrm() {
        initComponents();
        this.ObraTab.setEnabledAt(1, false);
        String timeStamp = new SimpleDateFormat("dd-MM-yyyy").format(Calendar.getInstance().getTime());
        this.dataObraText.setText(timeStamp);
        this.dataObraItemTxt.setText(timeStamp);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        criaCarroLabel = new javax.swing.JLabel();
        ObraTab = new javax.swing.JTabbedPane();
        obraTabPanel = new javax.swing.JPanel();
        matriculaLabel = new javax.swing.JLabel();
        matriculaTextF = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        kmsNaObraLabel = new javax.swing.JLabel();
        kmsNaObraText = new javax.swing.JTextField();
        dataObraLabel = new javax.swing.JLabel();
        dataObraText = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        descObraText = new javax.swing.JTextArea();
        descObraLabel = new javax.swing.JLabel();
        chassiObraLabel = new javax.swing.JLabel();
        chassiObraText = new javax.swing.JTextField();
        marcaObraLabel = new javax.swing.JLabel();
        marcaObraText = new javax.swing.JTextField();
        kmsCriacaoObraLabel = new javax.swing.JLabel();
        kmsCriacaoObraText = new javax.swing.JTextField();
        gravarBtn = new javax.swing.JButton();
        novoBtn = new javax.swing.JButton();
        messageLabel = new javax.swing.JLabel();
        materialTabPanel = new javax.swing.JPanel();
        materialLabel = new javax.swing.JLabel();
        materialTxt = new javax.swing.JTextField();
        refMaterialLabel = new javax.swing.JLabel();
        refMaterialTxt = new javax.swing.JTextField();
        dataObraItemLabel = new javax.swing.JLabel();
        dataObraItemTxt = new javax.swing.JTextField();
        adicionarMatBtn = new javax.swing.JButton();
        gravarObraItemBtn = new javax.swing.JButton();
        limparObraItemBtn = new javax.swing.JButton();
        messageLabelMaterial = new javax.swing.JLabel();
        messageItemLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Criar obra");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        criaCarroLabel.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        criaCarroLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/apresentacao/images/capacete2.png"))); // NOI18N
        criaCarroLabel.setText("Criar obra");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(criaCarroLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(criaCarroLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        ObraTab.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        matriculaLabel.setText("Matricula");

        matriculaTextF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                matriculaTextFFocusLost(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setText("*");

        kmsNaObraLabel.setText("Kms na obra");

        dataObraLabel.setText("Data criação");

        dataObraText.setEnabled(false);

        descObraText.setColumns(20);
        descObraText.setRows(5);
        jScrollPane1.setViewportView(descObraText);

        descObraLabel.setText("Descrição da obra");

        chassiObraLabel.setText("Chassi");

        chassiObraText.setEnabled(false);

        marcaObraLabel.setText("Marca");

        marcaObraText.setEnabled(false);

        kmsCriacaoObraLabel.setText("Kms criação");

        kmsCriacaoObraText.setEnabled(false);

        gravarBtn.setText("Gravar");
        gravarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gravarBtnActionPerformed(evt);
            }
        });

        novoBtn.setText("Novo");
        novoBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout obraTabPanelLayout = new javax.swing.GroupLayout(obraTabPanel);
        obraTabPanel.setLayout(obraTabPanelLayout);
        obraTabPanelLayout.setHorizontalGroup(
            obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(obraTabPanelLayout.createSequentialGroup()
                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(obraTabPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(descObraLabel)
                            .addGroup(obraTabPanelLayout.createSequentialGroup()
                                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(kmsNaObraLabel)
                                    .addGroup(obraTabPanelLayout.createSequentialGroup()
                                        .addComponent(matriculaLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(dataObraLabel))
                                .addGap(18, 18, 18)
                                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dataObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(kmsNaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(matriculaTextF, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(kmsCriacaoObraLabel)
                                    .addComponent(marcaObraLabel)
                                    .addComponent(chassiObraLabel))
                                .addGap(16, 16, 16)
                                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(chassiObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(marcaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(kmsCriacaoObraText, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jScrollPane1)))
                    .addGroup(obraTabPanelLayout.createSequentialGroup()
                        .addGap(135, 135, 135)
                        .addComponent(gravarBtn)
                        .addGap(28, 28, 28)
                        .addComponent(novoBtn))
                    .addGroup(obraTabPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        obraTabPanelLayout.setVerticalGroup(
            obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(obraTabPanelLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(matriculaLabel)
                    .addComponent(matriculaTextF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(chassiObraLabel)
                    .addComponent(chassiObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kmsNaObraLabel)
                    .addComponent(kmsNaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(marcaObraLabel)
                    .addComponent(marcaObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataObraLabel)
                    .addComponent(dataObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kmsCriacaoObraLabel)
                    .addComponent(kmsCriacaoObraText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(descObraLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(obraTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gravarBtn)
                    .addComponent(novoBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        ObraTab.addTab("Obra", obraTabPanel);

        materialLabel.setText("Material");

        refMaterialLabel.setText("Ref. Material");

        dataObraItemLabel.setText("Data criação");

        dataObraItemTxt.setEnabled(false);

        adicionarMatBtn.setText("Adicionar material");
        adicionarMatBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarMatBtnActionPerformed(evt);
            }
        });

        gravarObraItemBtn.setText("Gravar");
        gravarObraItemBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gravarObraItemBtnActionPerformed(evt);
            }
        });

        limparObraItemBtn.setText("Limpar");
        limparObraItemBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparObraItemBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout materialTabPanelLayout = new javax.swing.GroupLayout(materialTabPanel);
        materialTabPanel.setLayout(materialTabPanelLayout);
        materialTabPanelLayout.setHorizontalGroup(
            materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(materialTabPanelLayout.createSequentialGroup()
                .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(materialTabPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(refMaterialLabel)
                            .addComponent(materialLabel)
                            .addComponent(dataObraItemLabel))
                        .addGap(18, 18, 18)
                        .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(materialTabPanelLayout.createSequentialGroup()
                                .addComponent(dataObraItemTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(37, 37, 37)
                                .addComponent(adicionarMatBtn))
                            .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(materialTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                                .addComponent(refMaterialTxt))))
                    .addGroup(materialTabPanelLayout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(gravarObraItemBtn)
                        .addGap(18, 18, 18)
                        .addComponent(limparObraItemBtn))
                    .addGroup(materialTabPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(messageLabelMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(materialTabPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(messageItemLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        materialTabPanelLayout.setVerticalGroup(
            materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(materialTabPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(materialLabel)
                    .addComponent(materialTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(refMaterialLabel)
                    .addComponent(refMaterialTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataObraItemLabel)
                    .addComponent(dataObraItemTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(adicionarMatBtn))
                .addGap(44, 44, 44)
                .addGroup(materialTabPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gravarObraItemBtn)
                    .addComponent(limparObraItemBtn))
                .addGap(38, 38, 38)
                .addComponent(messageLabelMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
                .addComponent(messageItemLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        ObraTab.addTab("Material", materialTabPanel);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 102));
        jLabel2.setText("Software desenvolvido por SP. Todos os direitos reservados.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(ObraTab))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ObraTab)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limpaDadosFromText() {
        this.getMatriculaTextF().setText(null);
        this.getDataObraText().setText(null);
        this.getKmsNaObraText().setText(null);
        this.getKmsCriacaoObraText().setText(null);
        this.getChassiObraText().setText(null);
        this.getMarcaObraText().setText(null);
        this.getjTextArea1().setText(null);

    }
    
    private List<Carro> validaExisteMatricula() {
        carros = new ArrayList<Carro>();
        
        try {
        carroSevice = new CarroService();
        carros = carroSevice.viewCarro(this.matriculaTextF.getText());
        
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Ocorreu um erro na validação da matricula.","Erro",JOptionPane.ERROR_MESSAGE);
        }
        return carros;
    }
    
    private void getDadosFromTextObra () {
        obra = new Obra();
        
        obra.setCarro_id(carro.getCarro_id());
        obra.setKms_obra(this.kmsNaObraText.getText());
        obra.setDesc(this.descObraText.getText());      
    }
    
    private void getDadosFromObraItem() {
        obraItem = new Obra_Item();
        
        obraItem.setObra_id(obra.getObra_id());
        obraItem.setCarro_id(obra.getCarro_id());
        obraItem.setDesc_material(this.materialTxt.getText());
        obraItem.setRef_material(this.refMaterialTxt.getText());
    } 
    
    private void populaCamposCarro() {
        carro = carros.get(0);
        this.chassiObraText.setText(carro.getChassi());
        this.kmsCriacaoObraText.setText(carro.getKms());
        this.marcaObraText.setText(carro.getMarca());
    }
    
    private int checkCamposObrigatorios() {
        int check = 0; //0 = ok , 1 = NOK
        if (this.getMatriculaTextF().getText().equals("")) {
            //this.messageLabel.setForeground(Color.red);
            //this.messageLabel.setText("Os campos marcados com * são obrigatórios.");
            JOptionPane.showMessageDialog(this, "Os campos marcados com * são de preenchimento obrigatório.","Aviso",JOptionPane.WARNING_MESSAGE);
            check = 1;
        }
        return check;
    }
    
    private void limpaCamposMaterial() {
        this.refMaterialTxt.setText(null);
        this.materialTxt.setText(null);
    }
    
    private void gravarObraItemBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gravarObraItemBtnActionPerformed
         getDadosFromObraItem();
         
         try {
            obraService.criaObraItem(obra, obraItem, "Insert");
            this.messageItemLabel.setForeground(Color.BLUE);
            this.messageItemLabel.setText("Material criado com sucesso. Registo interno: "+String.valueOf(obraItem.getObra_item_id()));
         } catch (Exception e) {
             JOptionPane.showMessageDialog(this, "Ocorreu um erro ao inserir material.","Erro",JOptionPane.ERROR_MESSAGE);
         }
    }//GEN-LAST:event_gravarObraItemBtnActionPerformed

    private void gravarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gravarBtnActionPerformed
        int obrigatorio = 0;
        obraService = new ObraService();
        carros = new ArrayList<Carro>();
         
        obrigatorio = checkCamposObrigatorios();
        try {
            if (obrigatorio == 0) {
                getDadosFromTextObra();
                obraService.criaObra(obra, carro, "Insert");
                this.ObraTab.setEnabledAt(1, true);
                this.messageLabel.setForeground(Color.BLUE);
                this.messageLabel.setText("Obra criada com sucesso. Registo interno: "+String.valueOf(obra.getObra_id()));
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Ocorreu um erro a criar a obra.","Erro",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_gravarBtnActionPerformed

    private void matriculaTextFFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_matriculaTextFFocusLost
        carros = validaExisteMatricula();
        if (carros.size() == 1) {
            camposEnabled();
            populaCamposCarro();
            this.kmsNaObraText.requestFocus();
        }
        else {
            camposDisabled();
            JOptionPane.showMessageDialog(this, "A matricula não existe. Deve criar primeiro o carro antes de criar a obra.","Erro",JOptionPane.ERROR_MESSAGE);
            //this.matriculaTextF.requestFocus();
            this.matriculaTextF.setFocusable(true);
            return;
        }
    }//GEN-LAST:event_matriculaTextFFocusLost

    private void camposDisabled() {
        this.getKmsNaObraText().setEnabled(false);
        this.getjTextArea1().setEnabled(false);
        
    }
    
    private void camposEnabled() {
        this.getKmsNaObraText().setEnabled(true);
        this.getjTextArea1().setEnabled(true);
    }
    
    private void novoBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novoBtnActionPerformed
        limpaDadosFromText();
        this.ObraTab.setEnabledAt(1, false);
        this.matriculaTextF.requestFocus();
    }//GEN-LAST:event_novoBtnActionPerformed

    private void adicionarMatBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarMatBtnActionPerformed
       limpaCamposMaterial();
       this.materialTxt.requestFocus();
    }//GEN-LAST:event_adicionarMatBtnActionPerformed

    private void limparObraItemBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparObraItemBtnActionPerformed
         limpaCamposMaterial();
    }//GEN-LAST:event_limparObraItemBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ObraFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ObraFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ObraFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ObraFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ObraFrm().setVisible(true);
            }
        });
       
    }
public JTextField getChassiObraText() {
        return chassiObraText;
    }

    public void setChassiObraText(JTextField chassiObraText) {
        this.chassiObraText = chassiObraText;
    }

    public JTextField getKmsCriacaoObraText() {
        return kmsCriacaoObraText;
    }

    public void setKmsCriacaoObraText(JTextField kmsCriacaoObraText) {
        this.kmsCriacaoObraText = kmsCriacaoObraText;
    }

    public JTextField getMarcaObraText() {
        return marcaObraText;
    }

    public void setMarcaObraText(JTextField marcaObraText) {
        this.marcaObraText = marcaObraText;
    }
    
    public JTextField getDataObraItemTxt() {
        return dataObraItemTxt;
    }

    public void setDataObraItemTxt(JTextField dataObraItemTxt) {
        this.dataObraItemTxt = dataObraItemTxt;
    }

    public JTextField getDataObraText() {
        return dataObraText;
    }

    public void setDataObraText(JTextField dataObraText) {
        this.dataObraText = dataObraText;
    }

    public JTextArea getjTextArea1() {
        return descObraText;
    }

    public void setjTextArea1(JTextArea jTextArea1) {
        this.descObraText = jTextArea1;
    }

    public JTextField getKmsNaObraText() {
        return kmsNaObraText;
    }

    public void setKmsNaObraText(JTextField kmsNaObraText) {
        this.kmsNaObraText = kmsNaObraText;
    }

    public JTextField getMaterialTxt() {
        return materialTxt;
    }

    public void setMaterialTxt(JTextField materialTxt) {
        this.materialTxt = materialTxt;
    }

    public JTextField getMatriculaTextF() {
        return matriculaTextF;
    }

    public void setMatriculaTextF(JTextField matriculaTextF) {
        this.matriculaTextF = matriculaTextF;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane ObraTab;
    private javax.swing.JButton adicionarMatBtn;
    private javax.swing.JLabel chassiObraLabel;
    private javax.swing.JTextField chassiObraText;
    private javax.swing.JLabel criaCarroLabel;
    private javax.swing.JLabel dataObraItemLabel;
    private javax.swing.JTextField dataObraItemTxt;
    private javax.swing.JLabel dataObraLabel;
    private javax.swing.JTextField dataObraText;
    private javax.swing.JLabel descObraLabel;
    private javax.swing.JTextArea descObraText;
    private javax.swing.JButton gravarBtn;
    private javax.swing.JButton gravarObraItemBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel kmsCriacaoObraLabel;
    private javax.swing.JTextField kmsCriacaoObraText;
    private javax.swing.JLabel kmsNaObraLabel;
    private javax.swing.JTextField kmsNaObraText;
    private javax.swing.JButton limparObraItemBtn;
    private javax.swing.JLabel marcaObraLabel;
    private javax.swing.JTextField marcaObraText;
    private javax.swing.JLabel materialLabel;
    private javax.swing.JPanel materialTabPanel;
    private javax.swing.JTextField materialTxt;
    private javax.swing.JLabel matriculaLabel;
    private javax.swing.JTextField matriculaTextF;
    private javax.swing.JLabel messageItemLabel;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JLabel messageLabelMaterial;
    private javax.swing.JButton novoBtn;
    private javax.swing.JPanel obraTabPanel;
    private javax.swing.JLabel refMaterialLabel;
    private javax.swing.JTextField refMaterialTxt;
    // End of variables declaration//GEN-END:variables
}
